<template>
  <div class="loginWrap">
    <div class="header">
      <van-nav-bar
        title="修改账号密码"
        left-text="返回"
        left-arrow
        fixed
        @click-left="onClickLeft"
      ></van-nav-bar>
    </div>
    <div class="contentWrap">
      <div class="content">
        <div class="loginWrap">
          <van-form @submit="submit">
            <van-field
              v-model="username"
              name="用户名"
              label="用户名"
              placeholder="用户名"
              disabled
              :clearable="true"
            />
            <van-field
              v-model="password"
              type="password"
              name="新密码"
              label="新密码"
              placeholder="新密码"
              required
              :clearable="true"
              :rules="[{ required: true, message: '请填写密码' }]"
            />
            <van-field
              v-model="passwordConfirm"
              type="password"
              name="密码确认"
              label="密码确认"
              placeholder="密码确认"
              required
              :clearable="true"
              :rules="[{ required: true, message: '请填写确认密码' }]"
            />
            <div style="margin: 16px">
              <van-button round block type="info" native-type="submit"
                >保存</van-button
              >
            </div>
          </van-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// eslint-disable-next-line standard/object-curly-even-spacing
import { getUser, setPassword } from "@/views/api/api.js";
import { Toast, Dialog } from "vant";
export default {
  components: {},
  data() {
    return {
      username: "", // 用户名
      password: "", // 用户密码
      id: "",
      passwordConfirm: "", // 再次确认
    };
  },
  computed: {},
  created() {
    this.init();
  },
  methods: {
    init() {
      this.user = JSON.parse(sessionStorage.getItem("user"));
      if (this.user) {
        this.id = this.user[0]._id;
      } else {
        this.id = this.$route.query.id;
      }
      let params = {
        id: this.id,
      };
      getUser(params).then((res) => {
        if (!res.data.code) {
          this.username = res.data.Info[0].username;
          // this.password = res.data.Info[0].password
        } else {
          // 查询失败
          Toast.fail(res.data.msg);
        }
      });
    },
    // 点击保存
    submit() {
      if (this.password !== this.passwordConfirm) {
        Toast.fail("密码不一致");
      } else {
        let params = {
          username: this.username,
          password: this.password,
        };
        setPassword(params).then((res) => {
          if (res.data.code) {
            Toast.fail(res.data.msg);
          } else {
            this.init();
            this.$store.commit("setUser", res.data.data);
            this.$store.commit("setToken", res.data.data);
            Dialog.alert({
              message: "修改成功",
            }).then(() => {
              this.$router.replace({ path: "/login" });
            });
          }
        });
      }
    },
    // 点击返回
    onClickLeft() {
      this.$router.go(-1);
    },
  },
};
</script>
<style scoped>
.contentWrap {
  height: calc(100vh - 46px);
  overflow-y: auto;
}
/* 页面可以滚动，但是不出现滚动条 */
.contentWrap::-webkit-scrollbar {
  width: 0;
}
.header {
  height: 46px;
}
.content {
  margin: 0 16px;
}
.logo {
  width: 100%;
}
</style>
